"""
@name : mail
@author : zhangjian
@projectname: tlbb
"""

#邮件协议
#smtp协议 发邮件的协议  默认端口25号
#pop3  收邮件的协议   默认端口110
#imap 收邮件的协议   默认端口143

#pop3和imap都是收邮件的协议，区别在于
#pop3在客服端操作不会反馈到服务器
#imap在客户端的操作会反馈到服务器

#python里的两个模块发邮件
#1、smtplib  发邮件
#2、email  构建邮件内容

import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender_mail = 'dami_zhang@foxmail.com'
to = ['lovesc30@foxmail.com','343292019@qq.com']
sender_pass = 'jhbqvxxpytipbjid'
# 设置总的邮件体对象，对象类型为mixed
msg_root = MIMEMultipart('mixed')
# 邮件添加的头尾信息等
msg_root['From'] = 'dami_zhang@foxmail.com<dami_zhang@foxmail.com>'
msg_root['To'] = ','.join(to)
# 邮件的主题，显示在接收邮件的预览页面
subject = 'python sendemail test successful'
msg_root['subject'] = Header(subject, 'utf-8')
#构造图片
image_file = open(r'D:\个人\tlbb\comtrade\comtrade讲解\1.png','rb').read()
image = MIMEImage(image_file)
image.add_header('Content-ID','<image1>')
image['Content-Disposition'] = "attachment; filename='red_people.png'"
msg_root.attach(image)
try:
    sftp_obj = smtplib.SMTP('smtp.qq.com', 25)
    sftp_obj.login(sender_mail, sender_pass)
    sftp_obj.sendmail(sender_mail, to, msg_root.as_string())
    sftp_obj.quit()
    print('sendemail successful!')

except Exception as e:
    print('sendemail failed next is the reason')
    print(e)
